package com.hzaupj.znz.util;

import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;

/**
 * fragment
 * 
 * @author david
 */
public class FragmentUtils {

	/**
	 * 显示单一个fragment 移除已存在的frag
	 *
	 * @param ba
	 * @param fragment
	 * @param tag
	 */
	public static final void showSingle(FragmentActivity ba, Fragment fragment, String tag) {

		showSingle(ba.getSupportFragmentManager(), fragment, tag);
	}

	public static final Fragment findFragmentByTag(FragmentActivity ba, String tag) {
		FragmentManager manager = ba.getSupportFragmentManager();
		return manager.findFragmentByTag(tag);
	}

	/**
	 *
	 * 
	 * @param manager
	 * @param fragment
	 * @param tag
	 */
	public static final void showSingle(FragmentManager manager, Fragment fragment, String tag) {

//		manager.executePendingTransactions();
		Fragment pre = manager.findFragmentByTag(tag);
		FragmentTransaction ft = manager.beginTransaction();
		if (pre != null) {
			ft.remove(pre);
		}
		ft.add(fragment, tag);
		ft.commit();

	}
}
